package com.briup.smart.impl.client;

import com.briup.smart.env.client.Gather;
import com.briup.smart.env.entity.Environment;
import org.apache.commons.lang3.SerializationUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Timestamp;
import java.util.*;

public class GatherImpl implements Gather {
    @Override
    public Collection<Environment> gather() throws Exception {
        // 1、读取文件内容
        BufferedReader br = new BufferedReader(
                new InputStreamReader(
                        Objects.requireNonNull(GatherImpl.class.getClassLoader().getResourceAsStream("data-file"))));
        // 2、处理字符串
        String line = null;
        Collection<Environment> list = new LinkedList<>();
        while ((line = br.readLine()) != null) {
            String[] split = line.split("\\|");
            String srcId = split[0];
            String desId = split[1];
            String devId = split[2];
            String sensorAddress = split[3];
            int count = Integer.parseInt(split[4]);
            String cmd = split[5];
            int status = Integer.parseInt(split[7]);
            Timestamp gatherDate = new Timestamp(Long.parseLong(split[8]));

            // 3、创建集合以及使用数据创建Environment对象
            Environment environment = new Environment();
            environment.setSrcId(srcId);
            environment.setDesId(desId);
            environment.setDevId(devId);
            environment.setSensorAddress(sensorAddress);
            environment.setCount(count);
            environment.setCmd(cmd);
            environment.setStatus(status);
            environment.setGatherDate(gatherDate);

            // 要写一些判断
            // 根据判断定义名字
            String name = "";
            // 根据公式计算数据
            float data = -1;
            switch (sensorAddress) {
                case "16":
                    String wstring = split[6].substring(0,4);
                    String sstring = split[6].substring(4,8);
                    // 要在这里将温湿度拆为两个对象
                    name = "温度";
                    data = Integer.parseInt(wstring, 16) * (0.00268127F) - 46.85f;

                    String name2 = "湿度";
                    float data2 = Integer.parseInt(sstring, 16) * (0.00190735F) - 6f;
                    Environment environment2 = new Environment();
                    // 复制environment中的属性值给2
                    environment2 = SerializationUtils.clone(environment);
                    environment2.setName(name2);
                    environment2.setData(data2);
                    list.add(environment2);
                    break;
                case "256":
                    name = "光照强度";
                    data = (float) (Integer.parseInt(split[6].substring(0,4),16));

                    break;
                case "1280":
                    name = "二氧化碳浓度";
                    data = (float) (Integer.parseInt(split[6].substring(0,4),16));
                    break;
                default:
                    System.out.println("传感器地址错误");
            };
            environment.setName(name);
            environment.setData(data);
            // 添加到集合里
            list.add(environment);
        }
        // 记录一下采集的数量
        System.out.println("本次采集："+ list.size()+"条数据");
        return list;
    }
}
